package com.hdl.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import com.hdl.dao.MallOrderDao;
import com.hdl.dto.OrderSearch;
import com.hdl.entity.MallOrder;
import com.hdl.entity.MallOrderDetail;
import com.hdl.util.JdbcUtil;
import com.hdl.util.Druidutils;

public class MallOrderDaoImpl implements MallOrderDao{

	private JdbcTemplate template = new JdbcTemplate(Druidutils.getDataSource());
	
	private Connection con = null;
	private PreparedStatement pstm = null;
	private ResultSet rs = null;
	
	@Override
	public void saveOrder(Connection conn, MallOrder order) {
		try {
			//定义sql语句
			String sql = "insert into MALL_ORDER values(?,?,?,?,?,?,?,?,?,?,?) ";
			
			//设置连接对象
			con = conn;
			
			//获取预编译对象
			pstm = con.prepareStatement(sql);
			
			//赋值
			pstm.setObject(1, order.getMallOrderId());
			pstm.setObject(2, order.getUser().getHdlUserId());
			pstm.setObject(3, order.getMallOrderDate());
			pstm.setObject(4, order.getMallOrderCash());
			pstm.setObject(5, order.getMallOrderName());
			pstm.setObject(6, order.getMallOrderPhone());
			pstm.setObject(7, order.getMallOrderAddress());
			pstm.setObject(8, order.getMallOrderStatus());
			pstm.setObject(9, order.getMallOrderNum());
			pstm.setObject(10, null);//支付时间
			pstm.setObject(11, null);//发货时间
			
			//执行
			pstm.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void saveOrderDetails(Connection conn, MallOrderDetail detail) {
		try {
			//获取连接对象
			con = conn;
			
			//定义sql语句
			String sql = "insert into MALL_ORDER_DETAIL values(?,?,?,?,?)";
			
			//获取预编译对象
			pstm = con.prepareStatement(sql);
			
			//赋值
			pstm.setObject(1, detail.getMallOrderDetailId());
			pstm.setObject(2, detail.getMallOrderId()); 
			pstm.setObject(3, detail.getProduct().getMallProductId());
			pstm.setObject(4, detail.getMallOrderDetailCount());
			pstm.setObject(5, detail.getMallOrderDetailPrice());
			
			//执行
			pstm.executeUpdate();
		}catch(Exception e) {
			e.printStackTrace();
		}
	}

	
	@Override
	public void UpdateOrderStatus2(String mallOrderId,Date payforTime) {
		 
		//定义sql 语句
		String sql = "update MALL_ORDER set MALL_ORDER_STATUS = 2, MALL_ORDER_PAYFORTIME = ? WHERE MALL_ORDER_NUM = ?";
		
		template.update(sql,payforTime,mallOrderId);
	}

	@Override
	public List<MallOrder> findAllOrderByUserId(String hdlUserId, int orderStatus) {
		
		//定义sql语句
		String sql = "select * from MALL_ORDER where HDL_USER_ID = ? AND MALL_ORDER_STATUS = ? ";
		
		return template.query(sql, new BeanPropertyRowMapper<MallOrder>(MallOrder.class),hdlUserId,orderStatus);
	}

	@Override
	public MallOrder selectById(String mallOrderId) {
		
		//定义sql语句
		String sql = "select * from MALL_ORDER WHERE MALL_ORDER_ID = ? ";
		
		return template.queryForObject(sql, new BeanPropertyRowMapper<MallOrder>(MallOrder.class),mallOrderId);
	}

	@Override
	public void update(String mallOrderId, int i) {
		
		//定义sql语句
		String sql = "update MALL_ORDER set MALL_ORDER_STATUS = ? WHERE MALL_ORDER_ID = ?";
		template.update(sql,i,mallOrderId);
	}


}
